package com.hdu;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;

import static java.lang.Long.compare;

public class TimerTaskList implements Delayed {
    private final AtomicInteger TASK_COUNTER;
    private final TimerTaskEntry HEAD;
    private final AtomicLong EXPIRATION = new AtomicLong(-1L);


    public TimerTaskList(AtomicInteger TASK_COUNTER) {
        this.TASK_COUNTER = TASK_COUNTER;
        this.HEAD = new TimerTaskEntry(null, -1L);
        HEAD.next = HEAD;
        HEAD.prev = HEAD;
    }

    public boolean setExpiration(Long expirationMs) {
        return EXPIRATION.getAndSet(expirationMs) != expirationMs;
    }

    public Long getEXPIRATION() {
        return EXPIRATION.get();
    }


    public synchronized void foreach(Consumer<TimerTask> f) {
        TimerTaskEntry entry = HEAD.next;
        while (entry != HEAD) {
            TimerTaskEntry nextEntry = entry.next;
            if (!entry.cancel()) {
                f.accept(entry.timerTask);
            }
            entry = nextEntry;
        }
    }

    public void add(TimerTaskEntry timerTaskEntry) {
        boolean done = false;
        while (!done) {
            timerTaskEntry.remove();
            synchronized (this) {
                synchronized (timerTaskEntry) {
                    if (timerTaskEntry.list == null) {
                        TimerTaskEntry tail = HEAD.prev;
                        timerTaskEntry.next = HEAD;
                        timerTaskEntry.prev = tail;
                        timerTaskEntry.list = this;
                        tail.next = timerTaskEntry;
                        HEAD.prev = timerTaskEntry;
                        TASK_COUNTER.incrementAndGet();
                        done = true;
                    }
                }
            }
        }
    }

    private synchronized void remove(TimerTaskEntry timerTaskEntry) {
        synchronized (timerTaskEntry) {
            if (timerTaskEntry.list == this) {

                timerTaskEntry.next.prev = timerTaskEntry.prev;
                timerTaskEntry.prev.next = timerTaskEntry.next;

                timerTaskEntry.next = null;
                timerTaskEntry.prev = null;
                timerTaskEntry.list = null;

                TASK_COUNTER.decrementAndGet();
            }
        }
    }

    public synchronized void flush(Consumer<TimerTaskEntry> f) {
        TimerTaskEntry head = HEAD.next;
        while (head != HEAD) {
            remove(head);
            f.accept(head);
            head = HEAD.next;
        }
        EXPIRATION.set(-1L);
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(Math.max(getEXPIRATION() - System.currentTimeMillis(), 0), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed d) {
        TimerTaskList other = (TimerTaskList) d;
        return compare(getEXPIRATION(), other.getEXPIRATION());
    }

    public static class TimerTaskEntry implements Comparable<TimerTaskEntry> {

        public TimerTask timerTask;
        public Long expirationMs;
        private TimerTaskList list;
        private TimerTaskEntry prev;
        private TimerTaskEntry next;

        public TimerTaskEntry(TimerTask timerTask, Long expirationMs) {
            this.timerTask = timerTask;
            this.expirationMs = expirationMs;
            if (timerTask != null) {
                timerTask.setTimerTaskEntry(this);
            }
        }

        public boolean cancel() {
            return timerTask.getTimerTaskEntry() != this;
        }

        public void remove() {
            TimerTaskList currentList = list;
            while (currentList != null) {
                currentList.remove(this);
                currentList = list;
            }
        }

        @Override
        public int compareTo(TimerTaskEntry that) {
            return compare(expirationMs, that.expirationMs);
        }
    }
}

